﻿var vm = this.lookupViewModel()
    , expenseType = newValue
    , viewStatus = this.lookupController().analysis().status;

ref.SharingPrinciple.hide();
ref.companySharingRatioTips.hide();

//通过判断ExpenseType的值，给文本框控件赋必填权限,并且控制显示
//隐藏的控件名称,显示的控件名称的集合,(true:隐藏)  (false:显示)
var showOrHideFieldObj = {};
ref.UKCrossCharge.setValue('');

switch (expenseType) {

    case '1':
        showOrHideFieldObj['costCategory'] = true;
        showOrHideFieldObj['carLine'] = true;
        showOrHideFieldObj['budgetReallocation'] = true;

        showOrHideFieldObj['SharingPrinciple'] = false;
        showOrHideFieldObj['MaterialNumberCJLR'] = false;
        break;
    case '2':
        showOrHideFieldObj['carLine'] = true;
        showOrHideFieldObj['budgetReallocation'] = true;
        showOrHideFieldObj['costCategory'] = true;

        showOrHideFieldObj['SharingPrinciple'] = false;
        showOrHideFieldObj['MaterialNumberCJLR'] = false;
        break;
    case '3':
        showOrHideFieldObj['costCategory'] = true;
        showOrHideFieldObj['SharingPrinciple'] = true;
        showOrHideFieldObj['MaterialNumberCJLR'] = true;
        showOrHideFieldObj['BrandAllocationCJLR'] = true;
        showOrHideFieldObj['financeAnalystOwnerCJLR'] = true;

        showOrHideFieldObj['carLine'] = false;
        showOrHideFieldObj['budgetReallocation'] = false;
        break;
    default:
}

for (var ctrName in showOrHideFieldObj) {
    ref[ctrName].setHidden(showOrHideFieldObj[ctrName]);
}

//如果Company = LRE 则隐藏 costCategory
if (ref.companyC.getValue() == 'LRE') {
    ref.BrandAllocationJLRC.hide();
    ref.SharingPrinciple.hide();
    ref.companySharingRatioTips.hide();
    ref.costCategory.hide();
}

if (viewStatus == 'launch') {
    Ext.PAIRest.request({
        ns: PAI.application.url,
        url: 'last/dictItemObj/customQuery',
        method: "POST",
        async: false,
        data: {
            "fields": [
                { "name": "id" },
                {
                    "exp": "$." + PAI.Global.getLanguage(),
                    "name": "name",
                    "type": "json"
                },
                { "name": "code" },
                { "name": "orderIndex" }
            ],
            filter: "t1.pId='f4fabe6a-6362-4fdb-a54f-359e5e5b77ec'"
        },
        success: function (result) {
            var selectText = ref.ExpenseType.getRawValue();

            var data = result.data.filter(function (record) {
                return record.name == selectText;
            });
            if (data.length) {
                vm.set('NebulogyForm.othersSection.others.PurchasingEmp', data[0].code.replace(selectText + '#', ''));
            }
        }
    });
}

if (viewStatus == 'launch' || viewStatus == 'resubmit') {
    vm.set('NebulogyForm.launchEmployeeNo', PAI.Global.getAccount().employee.employeeNo);
}

var employeeNo = vm.get('NebulogyForm.launchEmployeeNo');

if (self.containsFocus) {
    ref.datagrid2.store.removeAll();
    ref.datagrid3.store.removeAll();
    ref.datagrid4.store.removeAll();
    ref.datagrid5.store.removeAll();

    ref.datagrid3.getColumns()[0].getEditor().store.data.removeAll();
    ref.datagrid4.getColumns()[0].getEditor().store.data.removeAll();
    ref.datagrid5.getColumns()[0].getEditor().store.data.removeAll();

    //SharingPrinciple中的项根据费用类型来联动.
    var code = '';
    switch (expenseType) {
        case '1':
        case '2':
        case '3':
            code = '7f9b174e-a940-4a86-b989-5f962e7e1622';
            break;
        default:
            break;
    }
    ref.SharingPrinciple.store.removeAll();
    ref.SharingPrinciple.store.code = code;
    ref.SharingPrinciple.store.load();

    var obj = {
        UKCrossCharge: '',
        PRType: '',
        costCategory: '',
        CIP: '',
        UKGLCode: '',
        UKCostCenter: '',
        VSVCheck: '',
        Check: '',
    };

    vm.set('NebulogyForm.FinanceInformation', obj);
    ref.SharingPrinciple.setValue('');
}

//根据登录用户编码和费用类型拉去 BudgetItems 数据
Ext.PAIRest.request({
    ns: PAI.application.serviceUrl,
    url: 'jlr-rest/api/budgetRelationItem/getBudgetItemByEmployeeNo',
    method: "POST",
    data: {
        employeeNo: employeeNo,
        expenseType: ref.ExpenseType.getRawValue(),
        valid: 'Y',
        imssNonimss: 'IMSS-Vehicle',
    },
    success: function (result) {
        var store = ref.BudgetItems.getStore();

        store.removeAll();

        Ext.each(result.data, function (record) {
            record.label = record.budgetitemDescription;
            record.value = record.id;
        });

        store.loadData(result.data);
        if (vm.get('NebulogyForm.GeneralInformation.BudgetItems')) {
            setTimeout(function () {
                ref.BudgetItems.setValue(vm.get('NebulogyForm.GeneralInformation.BudgetItems'));
            }, 100);
        }
        if (self.containsFocus) {
            ref.BudgetItems.setValue('');
            ref.Account.setValue('');
        }
    }
});